package com.hockeo.shared.domain.persistent;

import java.util.Date;

import com.extjs.gxt.ui.client.data.BeanModelTag;
import com.hockeo.client.metadata.IMetadataTag;
import com.hockeo.shared.domain.AbstractDomain;
import com.hockeo.tinygds.annotation.TEntity;
import com.hockeo.tinygds.annotation.TId;
import com.hockeo.tinygds.annotation.TName;

/**
 * Represents a manager (user) that participates in the game.
 *
 * @version $Id$
 * @author jjanke
 */
@TEntity
public class Manager extends AbstractDomain<Manager,String> implements IMetadataTag, BeanModelTag
{
  @TId
  private String d_strId;

  @TName( "user" )
  private String d_strEmail;

  @TName( "create_date" )
  private Date   d_dateCreated;

  @TName( "modified_date" )
  private Date   d_dateLastUpdated;

  @TName( "update_counter" )
  private int    d_nUpdateCounter;

  @TName( "last_locale" )
  private String d_strLastLocale;

  @TName( "last_ip" )
  private String d_strLastIP;

  /** Argument-less constructor for TinyGDS and GWT/RPC. */
  private Manager()
  {}

  /**
   * Creates a new user object.
   *
   * @param strUserId the Google unique user id
   * @param strEmail the user's email addres
   * @param strRemoteIP the IP address from which the user logged on last
   * @param strRemoteLocale the last used remote locale of the connected user
   */
  public Manager( String strUserId, String strEmail, String strRemoteIP, String strRemoteLocale )
  {
    d_strId = strUserId;

    update( strEmail, strRemoteIP, strRemoteLocale );

    d_dateCreated = d_dateLastUpdated;
  }

  /**
   * Updates this object with his current email address and the IP/locale used for the
   * last login.
   *
   * @param strEmail the user's email addres
   * @param strRemoteIP the IP address from which the user logged on last
   * @param strRemoteLocale the last used remote locale of the connected user
   */
  public void update( String strEmail, String strRemoteIP, String strRemoteLocale )
  {
    d_strEmail = strEmail;
    d_dateLastUpdated = new Date();
    d_strLastIP = strRemoteIP;
    d_strLastLocale = strRemoteLocale;
    d_nUpdateCounter++;
  }

  public String getEmail()
  {
    return d_strEmail;
  }

  public Date getDateCreated()
  {
    return d_dateCreated;
  }

  public Date getDateLastUpdated()
  {
    return d_dateLastUpdated;
  }

  public int getUpdateCounter()
  {
    return d_nUpdateCounter;
  }

  public String getLastLocale()
  {
    return d_strLastLocale;
  }

  public String getLastIP()
  {
    return d_strLastIP;
  }

  @Override
  public String getId()
  {
    return d_strId;
  }

  @Override
  public String getEntityName()
  {
    return "Manager";
  }

  @Override
  protected Object[][] propertiesForToString()
  {
    return new Object[][] { { "email", d_strEmail } };
  }
}
